/* com_writer.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>

#define BUFFER_SIZE 1024
char *dev = "/dev/ttyUSB0";

/*打开串口函数*/
int open_port(void)
{
	int fd;
	
	/* 打开串口 */
	fd = open(dev, O_RDWR|O_NOCTTY|O_NDELAY);
	if (fd < 0)
	{
		perror("open serial port");
		return(-1);
	}
	/*恢复串口为阻塞状态*/
	if (fcntl(fd, F_SETFL, 0) < 0)
	{
		perror("fcntl F_SETFL\n");
	}
	/*测试是否为终端设备*/
	if (isatty(STDIN_FILENO) == 0)
	{
		perror("standard input is not a terminal device");
	}
	return fd;
}
int set_com_config(int fd,int baud_rate,int data_bits, char parity, int stop_bits)
{
	struct termios new_cfg,old_cfg;
	int speed;
	/*保存并测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
	if (tcgetattr(fd, &old_cfg) != 0)
	{
		perror("tcgetattr");
		return -1;
	}
	/* 设置字符大小*/
	new_cfg = old_cfg;
	cfmakeraw(&new_cfg); /* 配置为原始模式 */
	new_cfg.c_cflag &= ~CSIZE;
	/*设置波特率*/
	switch (baud_rate)
	{
		case 2400:
			{
				speed = B2400;
			}
			break;
		case 4800:
			{
				speed = B4800;
			}
			break;
		case 9600:
			{
				speed = B9600;
			}
			break;
		case 19200:
			{
				speed = B19200;
			}
			break;
		case 38400:
			{
				speed = B38400;
			}
			break;
		default:
		case 115200:
			{
				speed = B115200;
			}
			break;
	}
	cfsetispeed(&new_cfg, speed);
	cfsetospeed(&new_cfg, speed);
	/*设置停止位*/
	switch (data_bits)
	{
		case 7:
			{
				new_cfg.c_cflag |= CS7;
			}
			break;
		default:
		case 8:
			{
				new_cfg.c_cflag |= CS8;
			}
			break;
	}
	/*设置奇偶校验位*/
	switch (parity)
	{
		default:
		case 'n':
		case 'N':
			{
				new_cfg.c_cflag &= ~PARENB;
				new_cfg.c_iflag &= ~INPCK;
			}
			break;
		case 'o':
		case 'O':
			{
				new_cfg.c_cflag |= (PARODD | PARENB);
				new_cfg.c_iflag |= INPCK;
			}
			break;
		case 'e':
		case 'E':
			{
				new_cfg.c_cflag |= PARENB;
				new_cfg.c_cflag &= ~PARODD;
				new_cfg.c_iflag |= INPCK;
			}
			break;
		case 's': /*as no parity*/
		case 'S':
			{
				new_cfg.c_cflag &= ~PARENB;
				new_cfg.c_cflag &= ~CSTOPB;
			}
			break;
	}
	/*设置停止位*/
	switch (stop_bits)
	{
		default:
		case 1:
			{
				new_cfg.c_cflag &= ~CSTOPB;
			}
			break;
		case 2:
			{
				new_cfg.c_cflag |= CSTOPB;
			}
	}
	/*设置等待时间和最小接收字符*/
	new_cfg.c_cc[VTIME] = 0;
	new_cfg.c_cc[VMIN] = 1;
	/*处理未接收字符*/
	tcflush(fd, TCIFLUSH);
	/*激活新配置*/
	if ((tcsetattr(fd, TCSANOW, &new_cfg)) != 0)
	{
		perror("tcsetattr");
		return -1;
	}
	return 0;
}

#define SEL_FILE_NUM 2
#define TIME_DELAY   100
int main(void)
{
	int res,i,real_read;
	int fds[SEL_FILE_NUM], recv_fd, maxfd;
	char buff[BUFFER_SIZE];
	fd_set inset,outset,tmp_inset;
	struct timeval tv;
	unsigned loop = 1;

	fds[0] = STDIN_FILENO;         /* 标准输入 */      
	if((fds[1] = open_port()) < 0) /* 打开串口 */
	{
		perror("open_port");
		return 1;
	}
	if(set_com_config(fds[1], 115200, 8, 'N', 1) < 0) /* 配置串口 */
	{
		perror("set_com_config");
		return 1;
	}

	FD_ZERO(&inset);                     //清除一个文件描述符集
	FD_ZERO(&outset);
	FD_SET(fds[0], &inset);              //将串口的文件描述符加入读文件描述符集合中
	FD_SET(fds[1], &inset);             //将标准输入文件描述符加入写文件描述符集合中
	maxfd = (fds[0] > fds[1])?fds[0]:fds[1];
	tv.tv_sec = 0;
	tv.tv_usec = 1;


	printf("fds[0]: %d , fds[1]: %d , maxfd: %d ", fds[0], fds[1], maxfd);
	
	printf("Input some words(enter 'quit' to exit):\n");

	while(loop && (FD_ISSET(fds[1], &inset) || FD_ISSET(fds[0], &inset)))
	{
		tmp_inset = inset;

		res = select(maxfd + 1, &tmp_inset, NULL, NULL, &tv);
		switch(res)
		{
			case -1:
				{
					perror("select\n");
					loop = 0;
				}
				break;
			case 0: /* Timeout */
				{
				//	perror("select time out\n");
				//	loop = 0;
				}
				break;
			default:
				{
					if (FD_ISSET(fds[1], &tmp_inset))
					{
						/* 将从串口读取的数据写入终端中 */
						real_read = read(fds[1], buff, BUFFER_SIZE);
						buff[real_read] = '\0';
						printf("The received words are : %s\n", buff); 
					}
					if (FD_ISSET(fds[0], &tmp_inset))
					{
						/* 将从终端读取的数据写入串口中 */
						real_read = read(fds[0], buff, BUFFER_SIZE);
						buff[real_read] = '\0';
						write(fds[1], buff, strlen(buff));
						printf("The send words are : %s\n", buff);
					}	
				}
		}
	}
//close(recv_fd);
//return 0;
}
